package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
@Service
@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationMapper tm;

    @Resource
    public TbSpecificationOptionMapper to;


    @Override
    public SpecVO toupdate(Long id) {
        SpecVO specVO = new SpecVO();
        TbSpecification i = tm.selectByPrimaryKey(id);
        specVO.setId(i.getId());
        specVO.setSpecname(i.getSpecName());
        List<TbSpecificationOption> list = to.selectBySpecId(id);
        specVO.setSpecOptionsList(list);
        return specVO;
    }

    @Transactional
    @Override
    public Integer update(SpecVO specVO) {
        TbSpecification t = new TbSpecification();
        t.setId(specVO.getId());
        t.setSpecName(specVO.getSpecname());
        tm.updateByPrimaryKeySelective(t);
        to.deleteBySpecId(specVO.getId());
        for (TbSpecificationOption option : specVO.getSpecOptionsList()) {
            to.insertSelective(option);
        }
        return 1;
    }

    @Override
    public List<TbSpecification> getAll1() {
        return tm.selectByExample(null);
    }

    @Override
    public int deleteCheck(Long[] ids) {
        List<Long> Llist = Arrays.asList(ids);
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Llist);
        tm.deleteByExample(example);
        for (int i = 0; i < Llist.size(); i++){
            Long aLong = Llist.get(i);
            TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria optionCriteria = optionExample.createCriteria();
            optionCriteria.andSpecIdEqualTo(aLong);
            to.deleteByExample(optionExample);
        }
        return 1;
    }

    @Transactional
    @Override
    public Integer saveSpecAndOptions(TbSpecification specification, List<TbSpecificationOption> list) {
        //先保存规格表并且返回主键
        tm.insertSelective(specification);
        Long id = specification.getId();
        //批量增加
        to.insertForEach(list,id);
        return 1;

    }

    @Override
    public PageBean<TbSpecification> getAll(Integer pageNum,Integer pageSize,String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbSpecification> tbSpecifications = tm.selectByName(name);
        PageInfo<TbSpecification> pageInfo = new PageInfo<>(tbSpecifications);
        return new PageBean<>(pageNum,pageSize,pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getList());
    }
}
